﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace Maydew.Windows.Forms.Controls.FlexiCell
{
    /// <summary>
    /// 具有边框的行标题。
    /// </summary>
    public class RowHeader : Shape
    {
        #region " 私有变量 "
        /// <summary>
        /// 获取或设置标题。
        /// </summary>
        private string _caption;
        /// <summary>
        /// 获取或设置行号。
        /// </summary>
        private int _rowIndex;
        /// <summary>
        /// 获取或设置整行选中。
        /// </summary>
        private bool _fullRowFocused;
        /// <summary>
        /// 获取或设置具有焦点。
        /// </summary>
        private bool _focused;
        /// <summary>
        /// 获取或设置字体。
        /// </summary>
        private Font _font;
        /// <summary>
        /// 获取或设置文本对齐样式。
        /// </summary>
        private ContentAlignment _captionAlign;
        #endregion

        #region " 构造函数 "
        /// <summary>
        /// 初始化 RowHeader 类的新实例。
        /// </summary>
        public RowHeader()
        {
            _font = SystemFonts.CaptionFont;
            _captionAlign = ContentAlignment.MiddleCenter;
        }
        #endregion

        #region " 属性 "
        /// <summary>
        /// 获取或设置标题。
        /// </summary>
        public string Caption
        {
            get
            {
                return _caption;
            }
            set
            {
                _caption = value;
            }
        }
        /// <summary>
        /// 获取或设置行号。
        /// </summary>
        public int RowIndex
        {
            get
            {
                return _rowIndex;
            }
            set
            {
                _rowIndex = value;
            }
        }
        /// <summary>
        /// 获取或设置整行选中。
        /// </summary>
        public bool FullRowFocused
        {
            get { return _fullRowFocused; }
            set { _fullRowFocused = value; }
        }
        /// <summary>
        /// 获取或设置具有焦点。
        /// </summary>
        public bool Focused
        {
            get { return _focused; }
            set { _focused = value; }
        }
        /// <summary>
        /// 获取或设置字体。
        /// </summary>
        public Font Font
        {
            get { return _font; }
            set { _font = value; }
        }
        /// <summary>
        /// 获取或设置文本对齐样式。
        /// </summary>
        public ContentAlignment CaptionAlign
        {
            get { return _captionAlign; }
            set { _captionAlign = value; }
        }
        #endregion

        #region " 绘制方法 "
        /// <summary>
        /// 绘制行标题。
        /// </summary>
        /// <param name="e">绘图参数。</param>
        public override void Painting(System.Windows.Forms.PaintEventArgs e)
        {
            if (base.Visible)
            {
                DrawingBackground(e);
                DrawingForeground(e);

                base.Painting(e);
            }
        }
        /// <summary>
        /// 绘制背景。
        /// </summary>
        /// <param name="e">绘图参数。</param>
        protected virtual void DrawingBackground(System.Windows.Forms.PaintEventArgs e)
        {
            Color color1 = Color.FromArgb(228, 236, 247);
            Color color2 = Color.FromArgb(228, 236, 247);
            Color borderColor = Color.FromArgb(158, 182, 206);
            if (Focused)
            {
                color1 = Color.FromArgb(255, 213, 141);
                color2 = Color.FromArgb(255, 213, 141);
                borderColor = Color.FromArgb(242, 149, 54);
            }
            if (FullRowFocused)
            {
                color1 = Color.FromArgb(157, 193, 228);
                color2 = Color.FromArgb(157, 193, 228);
                borderColor = Color.FromArgb(255, 255, 255);
            }
            using (Brush brush = new System.Drawing.Drawing2D.LinearGradientBrush(Bounds, color1, color2, 90f))
            {
                e.Graphics.FillRectangle(brush, Bounds);
            }
            using (Pen pen = new Pen(borderColor))
            {
                e.Graphics.DrawRectangle(pen, Bounds);
            }
        }
        /// <summary>
        /// 绘制前景。
        /// </summary>
        /// <param name="e">绘图参数。</param>
        protected virtual void DrawingForeground(System.Windows.Forms.PaintEventArgs e)
        {
            if (!string.IsNullOrEmpty(_caption))
            {
                Color color = Color.Black;
                using (Brush brush = new SolidBrush(color))
                {
                    Rectangle bound = Bounds;
                    RectangleF rect = new RectangleF(
                        Convert.ToSingle(bound.Left),
                        Convert.ToSingle(bound.Top),
                        Convert.ToSingle(bound.Width),
                        Convert.ToSingle(bound.Height));
                    StringFormat sf = new StringFormat(StringFormat.GenericDefault);
                    ConvertContentAlignment(sf, _captionAlign);

                    e.Graphics.DrawString(_caption, _font, brush, rect, sf);
                }
            }
        }
        #endregion
    }
}
